﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS Domestic CompanyManagement
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-1-27
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web.UI.WebControls;
using ASB.CC.Business.CIS;
using ASBCC.Business;
using ASBCC.Business.CIS;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.CIS2.Admin
{
    /// <summary>
    ///  CIS Domestic CompanyManagement
    /// </summary>
    public partial class CompanyManagement : EntityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override EntityDataSource EntitySource
        {
            get { return this.edsCompany; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return this.SearchText; }
        }

        protected override Button EntityAddBtn
        {
            get { return this.BtnAdd; }
        }

        protected override Button EntitySearchBtn
        {
            get { return this.Searchbtn; }
        }

        protected override Button EntityClearBtn
        {
            get { return this.Cleanbtn; }
        }

        protected override GridView EntityGrid
        {
            get { return this.gvCompany; }
        }

        protected override DetailsView EntityDetails
        {
            get { return this.dvCompany; }
        }

        protected override void OnEntityDisabling(int entityKey)
        {
            CompanyManagementHelper.SetCompanyDisabled(entityKey);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "Company disabled. id=" + entityKey + "; name=" + CompanyManagementHelper.getName(entityKey));

        }

        protected override string GetEntityWhereExpression(string keyword)
        {
            return "(it.Name like N'%" + keyword + "%'"
                 + " or it.Description like N'%" + keyword + "%')";
        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var company = e.Entity as Company;
            IPrincipal user = (IPrincipal)(Context.User);
            var pcatList = this.Request.Form["radioPCAT"];

            CompanyManagementHelper.OnCompanyManagementInserting(dc, company, pcatList, user);

        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var company = e.Entity as Company;
            IPrincipal user = (IPrincipal)(Context.User);
            var pcatList = this.Request.Form["radioPCAT"];

            CompanyManagementHelper.OnCompanyManagementUpdating(dc, company, pcatList, user);
        }



        /// <summary>
        /// 通过companyID取PCAT
        /// </summary>
        protected IEnumerable GetPCAT(object rawPCATs)
        {
            foreach (var pcat in rawPCATs as IEnumerable<PCAT>)
            {
                pcat.CategoryReference.Load();
                pcat.ProvinceReference.Load();
                yield return pcat;
            }
        }


        protected void edsCompany_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var company = e.Entity as Company;
            var pcats = LogInfoHelper.GetPCATWhthCompany(company.CompanyID);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "Company inserted. id=" + company.CompanyID + "; name=" + company.Name + "; description="+company.Description+"; pcat="+pcats);
        }

        protected void edsCompany_Updated(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var company = e.Entity as Company;
            var pcats = LogInfoHelper.GetPCATWhthCompany(company.CompanyID);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "Company updated. id=" + company.CompanyID + "; name=" + company.Name + "; description=" + company.Description + "; pcat=" + pcats);
        }

    }
}
